package cn.trda.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.trda.constants.WebConstants;
import cn.trda.pojo.Account;
import cn.trda.pojo.Accountdetail;
import cn.trda.pojo.Systemconfig;
import cn.trda.pojo.User;
import cn.trda.service.AccountDetailService;
import cn.trda.service.AccountService;
import cn.trda.service.SystemConfigService;
import cn.trda.service.Userservice;

@Controller
@RequestMapping("account")
public class AccountController extends BaseController{
	private static final Logger logger = Logger.getLogger(AccountController.class);
	@Autowired
	private Userservice asUserservice;
	@Autowired
	private SystemConfigService asSystemConfigService;
	@Autowired
	private AccountService asAccountService;
	@Autowired
	private AccountDetailService accountDetailService;
	
	@RequestMapping("/usercode/load")
	@ResponseBody
	public List<User> usercode_load(String usercode){
		
		
		return asUserservice.selectByusercode(usercode);
	}
	@RequestMapping("/manage")
	public String tofinance_manage(Model model){
		List<Systemconfig> list=asSystemConfigService.selectByConfigType(1);
		//System.out.println(asSystemConfigService.selectByConfigType(1));
		model.addAttribute("accountTypeList", list);
		return "finance_manage";
	}
	
	/**
	 * 代理商预付款页面
	 * advance_paument.jsp
	 * 根据指定条件查询自己用户的预付款情况
	 * @param page
	 * @param rows
	 * @param accountDetail
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="advancepay/list",method=RequestMethod.POST)
	public Map<String,Object> advancepayList(Integer page,Integer rows,Accountdetail accountDetail,HttpSession session) throws Exception{
		accountDetail.setUserid(((User)session.getAttribute(WebConstants.SESSION_USER_KEY)).getId());
		List<Accountdetail> list=accountDetailService.getAccountDetailList(accountDetail, page,rows);
		Integer count=accountDetailService.getCount(accountDetail);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("rows", list);
		map.put("total", count);
		return map;
	}
	@RequestMapping("/operation")
	@ResponseBody
	public String accountOperation(Accountdetail accountdetail,HttpSession session) throws Exception{
		
		Account account = asAccountService.selectByUserId(accountdetail.getUserid());
		if(account!=null){
			accountdetail.setDetaildatetime(new Date());
			if(asAccountService.insertSelective(account,accountdetail)==1){
				
				
				this.addLog(this.getSessionUser(session),
						accountdetail.getDetailtypename() + " : "
								+ accountdetail.getMemo());
				return "success";
			}else{
				return "fail";
			}
			
		}else{
			logger.error(accountdetail.getUserid() + "没有账号。");
			return "fail";
		}
	}
	/**
	 * 点击代理商预付款，跳转到advance_payment.jsp页面
	 * @return
	 */
	@RequestMapping(value="advancepay")
	public String advancepay (){
		return "advance_payment";
	}
	
	// 申请关键字成功后查询新的账户余额
		@ResponseBody
		@RequestMapping("money")
		public Object getAccount(HttpSession session) throws Exception {
			String result = WebConstants.OPERATION_MESSAGE_FAIL;
			Account account = new Account();
			User user = this.getSessionUser(session);
			account.setUserid(user.getId());
			account = asAccountService.getAccountByUserId(user.getId());
			if (account != null) {
				result = account.getMoney().toString();
			}
			return result;
		}
}
